import { defineStore } from 'pinia'

export const useUserStore = defineStore('user', {
  state: () => ({
    // 用户信息
    userInfo: {},
    // token
    token: '',
    // 菜单
    menu: [],
    // 是否加载菜单：用户登录重新加载
    dynamicCreateRoute: false,
    // 按钮权限
    permissions: [],
    // 数据字典
    dict: {},
    // 系统特殊字典：不可编辑删除、value取值value字段，其余取值pkid字段
    systemDictCode: [],
  }),
  getters: {
    getButtonPermissionByCode: (state) => {
      return (buttonCode) =>
        !!state.permissions?.find(
          (item) =>
            item.code === buttonCode &&
            item.status === 1 &&
            item.showStatus === 1,
        )
    },
    getButonNameByCode: (state) => {
      return (buttonCode) => {
        const button = state.permissions.find(
          (item) => item.code === buttonCode,
        )
        return button ? button.catalogName : ''
      }
    },
  },
  actions: {
    // 设置用户信息
    setUserInfo(userInfo) {
      this.userInfo = userInfo
    },
    // 设置token
    setToken(token) {
      this.token = token
    },
    // 获取数据字典
    setDict(data) {
      this.systemDictCode = [
        'serviceType',
        'status',
        'yn',
        'publishStatus',
        'reservationExamineStatus',
        'balckStatus',
        'menuType',
        'role_data_manage',
        'inspection_status',
        'inspection_plan_point_allocation_type',
        'inspection_inspection_type',
        'inspection_plan_type',
        'special_task_type',
        'video_watch_duration_unit',
        'special_task_publish_scope',
        'inspector_post_type',
        'user_type',
        'inspection_inspection_scene',
        'inspection_check_project_type',
        'task_status',
        'inspection_point_item_status',
        'warning_status',
        'device_status',
        'rectification_notice_template',
        'warning_process_status',
        'isPass',
        'rectification_status',
        'inspection_point_item_process',
        'is_read',
        'main_post',
        'car_plate_category',
        'car_plate_color',
        'car_plate_property',
        'gender',
        'tcfwlx',
      ]
      for (let key in data) {
        if (!this.systemDictCode.includes(key)) {
          data[key].forEach((dataItem) => {
            dataItem.value = dataItem.pkid
          })
        }
      }
      this.dict = data
    },
    // 获取菜单
    setMenu(data) {
      // const home = {
      //   code: 'home',
      //   routePath: '/home',
      //   catalogName: '首页',
      //   routeModule: 'Layout',
      // }
      // data.splice(0, 0, home)
      this.menu = data
    },
    // 获取按钮权限
    setPermissions(data) {
      this.permissions = data
    },
    // 获取是否加载菜单
    setDynamicCreateRoute(data) {
      this.dynamicCreateRoute = data
    },
    // 清除
    clearUser() {
      this.userInfo = {}
      this.token = ''
      this.dict = {}
      this.menu = []
      this.permissions = []
      this.dynamicCreateRoute = false
      this.systemDictCode = []
    },
  },
  persist: true,
})
